home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TA2ASV08.ZIP / TA2AS.C < prev    next >
C/C++ Source or Header  |  1995-01-17  |  6KB  |  370 lines

  1. /* TA2AS CONVERTER MODULE,
  2.    COPYRIGHT 1994 FRANK VAN DIJK, LAST UPDATE M6-21-94    11:05
  3. */
  4.  
  5. #include "ta2as.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. char rarray[38][4]=
  10. {
  11. "ah\0",
  12. "al\0",
  13. "ax\0",
  14. "bh\0",
  15. "bl\0",
  16. "bp\0",
  17. "bx\0",
  18. "ch\0",
  19. "cl\0",
  20. "cr0",
  21. "cr1",
  22. "cr2",
  23. "cr3",
  24. "cx\0",
  25. "dh\0",
  26. "di\0",
  27. "dl\0",
  28. "dr0",
  29. "dr1",
  30. "dr2",
  31. "dr3",
  32. "dr4",
  33. "dr5",
  34. "dr6",
  35. "dr7",
  36. "dx\0",
  37. "eax",
  38. "ebp",
  39. "ebx",
  40. "ecx",
  41. "edx",
  42. "edi",
  43. "esi",
  44. "esp",
  45. "si\0",
  46. "sp\0",
  47. "tr6",
  48. "tr7"
  49. };
  50.  
  51. #define numofmods 8
  52. modstruct mods[numofmods]=
  53. {
  54. {"dosseg\0",NULL},
  55. {"end\0\0\0\0",NULL},
  56. {"equ\0\0\0\0",EquReshuffle},
  57. {"extrn\0\0",NULL},
  58. {"ideal\0\0",NULL},
  59. {"model\0\0",NULL},
  60. {"p386\0\0\0",NULL},
  61. {"stack\0\0",NULL},
  62. };
  63.  
  64. #define numofreps 19
  65. char replace[numofreps*2][8]=
  66. {
  67. "cbw","cbtw",
  68. "cdq","cltd",
  69. "cmpsd","cmpsl",
  70. "codeseg",".text",
  71. "cwd","cwtd",
  72. "cwde","cwtl",
  73. "dataseg",".data",
  74. "db",".byte",
  75. "dd",".int",
  76. "dw",".short",
  77. "insd","insl",
  78. "lodsd","lodsl",
  79. "movsd","movsl",
  80. "movsx","movs",
  81. "movzx","movz",
  82. "outsd","outsl",
  83. "public",".globl",
  84. "scasd","scasl",
  85. "stosd","stosl",
  86. };
  87.  
  88. void EquReshuffle(cbuf lab,cbuf com,cbuf rem,oprd *op,int hoeveel) 
  89. {
  90. strcpy(com,lab);
  91. strcat(com,",");
  92. strcpy(lab,".equ");
  93. }
  94.  
  95. int ReplaceMissch(cbuf lab,cbuf com,cbuf rem,oprd *op,int hoeveel)
  96. {
  97. char *p;
  98. if(p=bsearch(com,replace,numofreps,16,(void*)stricmp))
  99.     {
  100.     strcpy(com,p+8);
  101.     return 0;
  102.     }
  103. if (p=bsearch(lab,replace,numofreps,16,(void*)stricmp))
  104.     {
  105.     strcpy(lab,p+8);
  106.     return 1;
  107.     }
  108. }
  109.  
  110. void ModLine(cbuf lab,cbuf com,cbuf rem,oprd *op,int *hoeveel)
  111. {
  112. modstruct *p;
  113. int waslabel;
  114. if(p=bsearch(com,mods,numofmods,sizeof(modstruct),(void *)stricmp))
  115.     {
  116.     waslabel=0;
  117.     goto roepmodder;
  118.     }
  119. else if(p=bsearch(lab,mods,numofmods,sizeof(modstruct),(void *)stricmp))
  120.     {
  121.     waslabel=1;
  122. roepmodder:
  123.     if(p->p)
  124.         {
  125.         if(p->p==EquReshuffle) waslabel=1; /*ieuw*/
  126.         p->p(lab,com,rem,op,*hoeveel);
  127.         }
  128.     else *lab=*com=*hoeveel=0;
  129.     }
  130. else waslabel=ReplaceMissch(lab,com,rem,op,*hoeveel);
  131. if(!waslabel && (waslabel=strlen(lab)) && *((lab+=waslabel)-1)!=':')
  132.     {
  133.     *(lab)=':';
  134.     *(lab+1)='\0';
  135.     }
  136. }
  137.  
  138. int IsReg(cbuf op)
  139. {
  140. cbuf buf;
  141. int len;
  142. char temp;
  143. strncpy(buf,op,len=strcspn(op," \t"));
  144. *(buf+len)=0;
  145. return (int)bsearch(strlwr(buf),rarray,38,4,(void *)strcmp);
  146. }
  147.  
  148. void RemoveWs(cbuf op)
  149. {
  150. int len=strlen(op);
  151. do
  152.     {
  153.     if(*op==' '||*op=='\t')
  154.         {
  155.         memmove(op,op+1,--len+1);
  156.         }
  157.     } while(*(op++)!=0);
  158. }
  159.  
  160. void CrackMemInd(oprd *op)
  161. {
  162. int len;
  163. cbuf disp,base,ofs,multi;
  164. char temp;
  165. char oldchar[2];
  166. char *here,*opg;
  167. disp[0]=multi[0]=0;
  168. ((unsigned short*)base)[0]=((unsigned short*)ofs)[0]=37;
  169. ((unsigned short*)oldchar)[0]=0;
  170. opg=op->op;
  171. opg++;
  172. opg+=strspn(opg," \t");
  173. len=*((int *)opg)&0xdfdfdfdf;
  174. if(len==*(int*)"BYTE" && isspace(*(opg+4)))
  175.     {
  176.     op->flags|=1;
  177.     opg+=5;
  178.     }
  179. else if(len==*(int*)"WORD" && isspace(*(opg+4)))
  180.     {
  181.     op->flags|=2;
  182.     opg+=5;
  183.     }
  184. else if(len==*(int*)"DWOR" && (*(opg+4)&0xdf)=='D' && isspace(*(opg+5)))
  185.     {
  186.     op->flags|=4;
  187.     opg+=6;
  188.     }
  189. RemoveWs(opg);
  190. do
  191.     {
  192.     len=strcspn(opg,"+-]");
  193.     temp=*(opg+len);
  194.     *(opg+len)=0;
  195.     if(here=strchr(opg,'*'))
  196.         {
  197.         *here=0;
  198.         if(IsReg(opg))
  199.             {
  200.             strcpy(ofs+1,opg);
  201.             strcpy(multi,here+1);
  202.             }
  203.         else if(IsReg(here+1))
  204.             {
  205.             strcpy(ofs+1,here+1);
  206.             strcpy(multi,opg);
  207.             }
  208.         else
  209.             {
  210.             *here='*';
  211.             goto concatdisp;
  212.             }
  213.         }
  214.     else
  215.         {
  216.         if(IsReg(opg))
  217.             {
  218.             if(*(base+1)) strcpy(ofs+1,opg);
  219.             else strcpy(base+1,opg);
  220.             }
  221.         else
  222.             {
  223. concatdisp:
  224.             strcat(disp,oldchar);
  225.             strcat(disp,opg);
  226.             }
  227.         }
  228.     if(temp==']') break;
  229.     *oldchar=temp;
  230.     opg+=len+1;
  231.     } while(1);
  232. opg=op->op;
  233. *opg=0;
  234. strcat(opg,*disp=='+'?disp+1:disp);
  235. if(*(base+1)||*(ofs+1))
  236.     {
  237.     strcat(opg,"(");
  238.     if (*(base+1)) strcat(opg,base);
  239.     if (*(ofs+1))
  240.         {
  241.         strcat(opg,",");
  242.         strcat(opg,ofs);
  243.         if(*multi)
  244.             {
  245.             strcat(opg,",");
  246.             strcat(opg,multi);
  247.             }
  248.         }
  249.     strcat(opg,")");
  250.     }
  251. }
  252.  
  253. void HexBin(oprd *op)
  254. {
  255. int len,len2;
  256. char *opg=op->op;
  257. while(len=strcspn(opg,"0123456789"),opg+=len,isdigit(*opg))
  258.     {
  259.     len2=strspn(opg,"0123456789abcdefABCDEF");
  260.     if(len && (isalpha(*(opg-1)) || *(opg-1)=='@' || *(opg-1)=='_'))
  261.         {
  262.         }
  263.     else
  264.         {
  265.         char ch;
  266.         if((*(opg+len2)&0xdf)=='H')
  267.             {
  268.             ch='x';
  269. frot:
  270.             memmove(opg+len2+1,opg+len2,strlen(opg+len2)+1);
  271.             memmove(opg+2,opg,len2);
  272.             *opg='0';
  273.             *(++opg)=ch;
  274.             }
  275.         else if((*(opg+len2-1)&0xdf)=='B')
  276.             {
  277.             len2--;
  278.             ch='B';
  279.             goto frot;
  280.             }
  281.         }
  282.     opg+=len2;
  283.     }
  284. }
  285.  
  286. int Operand2Operand(oprd *op)
  287. {
  288. char *temp,*opg;
  289. opg=op->op;
  290. opg+=strspn(opg," \t");
  291. if(*opg=='[')
  292.     {
  293.     CrackMemInd(op);
  294.     }
  295. else if(!strnicmp(opg,"offset",6))
  296.     {
  297.     temp=opg+6;
  298.     temp+=strspn(temp," \t");
  299.     *--temp='$';
  300.     memmove(opg,temp,strlen(temp)+1);
  301.     }
  302. else if(IsReg(opg))
  303.     {
  304.     int len=strcspn(opg," \t");
  305.     if (len==2)
  306.         {
  307.         if (opg[len-1]=='l' || opg[len-1]=='h') op->flags|=1;
  308.         else op->flags|=2;
  309.         }
  310.     else op->flags|=4;
  311.     memmove(opg+1,opg,len+1);
  312.     *opg='%';
  313.     }
  314. else
  315.     {
  316.     memmove(opg+1,opg,strlen(opg)+1);
  317.     *opg='$';
  318.     }
  319. HexBin(op);
  320. {
  321. int len=strlen(opg)-1;
  322. while(opg[len]==' '||opg[len]=='\t')
  323.   opg[len--]=0;
  324. }
  325. return 1;
  326. }
  327.  
  328. int ChopEm(cbuf in,cbuf lab,cbuf com,cbuf rem,oprd *op,int *hoeveel)
  329. {
  330. char *p;
  331. int len,teller=0;
  332. if(!isspace(in[0])) /*label*/
  333.     {
  334.     if(*in==';') goto restisremark;
  335.     len=strcspn(in," \t;");
  336.     strncpy(lab,in,len);
  337.     *(lab+len)=0;
  338.     in+=len;
  339.     }
  340. else *lab=0;
  341.  
  342. in+=strspn(in," \t");
  343. if(*in==';') goto restisremark;
  344. len=strcspn(in," \t;");
  345. strncpy(com,in,len);
  346. *(com+len)=0;
  347. in+=len;
  348. if(*in==';')
  349.     goto restisremark;
  350. in+=strspn(in," \t");
  351. while(*in!=0 && *in!=';'&& teller<32)
  352.     {
  353.     len=strcspn(in,",;");
  354.     strncpy(op[teller].op,in,len);
  355.     *(op[teller++].op+len)=0;
  356.     /*if (*(in+len)==',') len++;*/
  357.     in+=strspn(in+=len," \t,");
  358.     }
  359.  
  360. if (*in==';')
  361. restisremark:
  362.     {
  363.     *in='#';
  364.     strcpy(rem,in);
  365.     }
  366. *hoeveel=teller;
  367. return 1;
  368. }
  369.  
  370.